package com.example.healthapp.ui.fragment;

import android.annotation.SuppressLint;
import androidx.fragment.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.healthapp.R;
import com.example.healthapp.utils.InterfaceTool;
import com.example.healthapp.utils.SPManage;

import java.util.Random;

// 我的页面的Fragment
public class MineFragment extends Fragment {

    View view;
    ImageView fgt4_head_img;
    CardView fgt4_progress_card, fgt4_height_card, fgt4_weight_card, fgt4_heart_card;
    TextView fgt4_heart, fgt4_weight, fgt4_height;
    String head[] = {
            "https://cdn.pixabay.com/photo/2021/05/22/06/07/woman-6272635_1280.jpg",
            "https://cdn.pixabay.com/photo/2021/03/03/08/56/woman-6064819_1280.jpg",
            "https://cdn.pixabay.com/photo/2021/01/20/08/57/woman-5933499_1280.png",
            "https://cdn.pixabay.com/photo/2021/02/20/11/00/woman-6032953_1280.png",
            "https://cdn.pixabay.com/photo/2021/01/17/09/11/woman-5924366_1280.jpg",
            "https://cdn.pixabay.com/photo/2021/06/07/13/46/man-6318010_1280.jpg",
            "https://cdn.pixabay.com/photo/2021/03/03/10/17/man-6064964_1280.jpg",
            "https://cdn.pixabay.com/photo/2023/06/16/15/10/man-ai-8068201_1280.jpg",
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_mine, container, false);
            initview();
            event();
            refreshView();
        }
        return view;
    }

    private void refreshView() {
        InterfaceTool.setAnimator(fgt4_weight_card, new float[]{0}, new float[]{20, 0, -20, 0, 20}, new float[]{1f}, new float[]{1}, new float[]{0.7f, 1f, 0.7f}, new float[]{0}, 2000, -1);
        InterfaceTool.setAnimator(fgt4_heart_card, new float[]{0}, new float[]{25, 0, -25, 0, 25}, new float[]{1f}, new float[]{1}, new float[]{0.7f, 1f, 0.7f}, new float[]{0}, 2800, -1);
        InterfaceTool.setAnimator(fgt4_height_card, new float[]{0}, new float[]{20, 0, -20, 0, 20}, new float[]{1f}, new float[]{1}, new float[]{0.7f, 1f, 0.7f}, new float[]{0}, 2300, -1);
        // 在你的代码中使用Glide加载图片并设置圆角
        RequestOptions requestOptions = new RequestOptions()
                .transform(new RoundedCorners(80)); // 这里的30表示圆角的半径，你可以根据需要调整
        Glide.with(getContext())
                .load("https://cdn.pixabay.com/photo/2021/03/03/10/17/man-6064964_1280.jpg")
                .apply(requestOptions)
                .into(fgt4_head_img);
    }

    private void event() {
        fgt4_head_img.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                RequestOptions requestOptions = new RequestOptions()
                        .transform(new RoundedCorners(80)); // 这里的30表示圆角的半径，你可以根据需要调整
                Glide.with(getContext())
                        .load(head[new Random().nextInt(head.length)])
                        .apply(requestOptions)
                        .into(fgt4_head_img);
                return false;
            }
        });
    }

    @SuppressLint("UnspecifiedRegisterReceiverFlag")
    private void initview() {
        fgt4_head_img = view.findViewById(R.id.fgt4_head_img);
        fgt4_progress_card = view.findViewById(R.id.fgt4_progress_card);
        fgt4_heart = view.findViewById(R.id.fgt4_heart);
        fgt4_weight = view.findViewById(R.id.fgt4_weight);
        fgt4_height = view.findViewById(R.id.fgt4_height);
        fgt4_height_card = view.findViewById(R.id.fgt4_height_card);
        fgt4_weight_card = view.findViewById(R.id.fgt4_weight_card);
        fgt4_heart_card = view.findViewById(R.id.fgt4_heart_card);
    }

    @Override
    public void onResume() {
        if (fgt4_progress_card != null) {
            fgt4_heart.setText(SPManage.getSP(getContext(), "health").getString("heart"));
            fgt4_weight.setText(SPManage.getSP(getContext(), "health").getString("weight"));
            fgt4_height.setText(SPManage.getSP(getContext(), "health").getString("height"));
            InterfaceTool.setAnimator(fgt4_progress_card, new float[]{0}, new float[]{0}, new float[]{0, 0.8f}, new float[]{1}, new float[]{0.5f, 1f}, new float[]{0}, 1500, 0, 0f);
        }
        super.onResume();
    }
}